/**
 * 
 */
package com.ctg.payroll2.service.cusadmin.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.po.CsAccount;
import com.ctg.payroll2.bean.po.CsEmpAccount;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.dao.cusadmin.CsAccountDAO;
import com.ctg.payroll2.dao.empadmin.EmpAdminIndexDao;
import com.ctg.payroll2.exception.AccountHasExistedException;
import com.ctg.payroll2.service.BaseService;
import com.ctg.payroll2.service.cusadmin.CsAccountService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.core.security.Exception.SecurityException;
import com.ctg.platform.core.security.entity.Account;
import com.ctg.platform.util.Page;

/**
 * @author 郑致力
 *
 */
public class CsAccountServiceImpl extends BaseService implements CsAccountService {
	private CsAccountDAO csAccountDAO;
	private SpringSecurityManager springSecurityManager;
	

	public void addSystemAdminAccount(String username, String password, String display_name, SysUser user) throws AccountHasExistedException, SQLException {		
		//创建Account,并赋予系统管理员的权限
		Account account = new Account(username,password,Constants.DEFAULT_STATE[0]);
		try {
			account = springSecurityManager.createSysAccountAndGet(account, user);
		} catch (SecurityException se) {
			throw new AccountHasExistedException();
		}
		springSecurityManager.assignRoleForAccount(account, new String[]{Constants.DEFAULT_ROLE[1]}, user);
		
		//创建Cs_Account,并保存
		CsAccount csAccount = new CsAccount();
		csAccount.setUser_id(account.getId());
		csAccount.setDisplay_name(display_name);
		csAccount.setData_come(Constants.DEFAULT_DATASOURCE[0]);
		
		csAccountDAO.insert(csAccount);	
	}
	
	public Page queryWithPaginationByAdmin(String user_code, String company_chinese_code, Boolean isSystemAdmin, Boolean isFreeze, int start, int limit) {
		Map<String,Object> parameters = new HashMap<String,Object>();
		if (user_code != null) {
			parameters.put("user_code", user_code);
		}
		if (company_chinese_code != null) {
			parameters.put("company_chinese_code", company_chinese_code);
		}	
		if (isSystemAdmin != null) {
			parameters.put("isSystemAdmin", isSystemAdmin);
		}
		if (isFreeze != null) {
			if (isFreeze == Boolean.TRUE) {
				parameters.put("disabled", Constants.DEFAULT_STATE[0]); 
			} 
			if (isFreeze == Boolean.FALSE) {
				parameters.put("disabled", Constants.DEFAULT_STATE[1]); 
			} 
		}
		parameters.put("data_come", 0);
		return csAccountDAO.pagedQuery(parameters, start, limit);
	}
	
	public void modifyAdminAccount(String user_id , String displayName) throws SQLException {
		CsAccount account = csAccountDAO.findByPrimaryKey(user_id);
		if (account != null) {
			account.setDisplay_name(displayName);
		}
		csAccountDAO.update(account);
	}
	private EmpAdminIndexDao empAdminIndexDao;
	public EmpAdminIndexDao getEmpAdminIndexDao() {
		return empAdminIndexDao;
	}

	public void setEmpAdminIndexDao(EmpAdminIndexDao empAdminIndexDao) {
		this.empAdminIndexDao = empAdminIndexDao;
	}

	public void addClientSystemAdminAccount(String company_code, String username, String password, String displayName, SysUser user) throws AccountHasExistedException, SQLException {
		if (company_code == null) {
			return;
		}
		//创建Account,并赋予客户管理员的权限
		Account account = new Account(username,password,Constants.DEFAULT_STATE[1]);
		try {
			account = springSecurityManager.createSysAccountAndGet(account, user);
		} catch (SecurityException se) {
			throw new AccountHasExistedException();
		}
		springSecurityManager.assignRoleForAccount(account, new String[]{Constants.DEFAULT_ROLE[1]}, user);
		
		//创建Cs_Account,并保存
		CsAccount csAccount = new CsAccount();
		csAccount.setCompany_code(company_code);
		csAccount.setUser_id(account.getId());
		csAccount.setDisplay_name(displayName);
		csAccount.setData_come(Constants.DEFAULT_DATASOURCE[0]);
		
		csAccountDAO.insert(csAccount);
		CsEmpAccount csEmpAccount = new CsEmpAccount();
		SysUser s = (SysUser) ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
		csEmpAccount.setCompany_code(company_code);
		csEmpAccount.setLogin_code(account.getId());
		
		empAdminIndexDao.insertempRoll(csEmpAccount);
	}

	public CsAccountDAO getCsAccountDAO() {
		return csAccountDAO;
	}

	public void setCsAccountDAO(CsAccountDAO csAccountDAO) {
		this.csAccountDAO = csAccountDAO;
	}

	public SpringSecurityManager getSpringSecurityManager() {
		return springSecurityManager;
	}

	public void setSpringSecurityManager(SpringSecurityManager springSecurityManager) {
		this.springSecurityManager = springSecurityManager;
	}
}
